﻿
using Model.lagou;
using SpiderLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleSpider
{
    class Program
    {
        
        static void Main(string[] args)
        {




            //"Java",
            //后端开发
            string[] langArray = {"Python","PHP",".NET","C#","C++","C","VB","Delphi","Perl","Ruby","Hadoop","Node.js","数据挖掘","自然语言处理","搜索算法","精准推荐","全栈工程师","Go","ASP","Shell","后端开发其它"};
            //List<Root> listRoot = new List<Root>();

            for (int j = 0; j < langArray.Count(); j++)
            {
                Task task = Task.Factory.StartNew(() => LoadCate(langArray[j]));
            }
         


        
            Console.ReadLine();

        }

        private static void LoadCate(string langArray)
        {



            string searchdata = "city=全国&kd=" + langArray;
            Console.WriteLine("正在加载" + langArray + "");
            Root root = LoadData(1, searchdata);
            Console.WriteLine("加载成功_共" + root.content.totalPageCount + "页");
            if (root.content != null && root.content.totalPageCount > 0)
            {
                for (int i = 1; i < root.content.totalPageCount; )
                {
                    i++;
                    Task<Root> task = Task.Factory.StartNew<Root>(() => LoadData(i, searchdata));
                    Root taskResult = task.Result;


                }
            }
        }
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="i"></param>
        /// <param name="searchdata"></param>
        /// <param name="listRoot"></param>
        public static Root LoadData(int i, string searchdata)
        {
            HttpHelper httpHelper = new HttpHelper();
            HttpItem httpItem = new HttpItem();
            Console.WriteLine("正在加载" + i + "页");
            httpItem.URL = "http://www.lagou.com/jobs/positionAjax.json?" + string.Format(searchdata + "&pn={0}", i);
            HttpResult itemResult = httpHelper.GetHtml(httpItem);

            Root root = JsonTools.Deserialize<Root>(itemResult.Html);
            insertData(root);
            return root;


        }
        static MySqlHelper sqlHelper = new MySqlHelper();
        public static void insertData(Root root)
        {
            if (root!=null&&root.content!=null&&root.content.totalPageCount>0)
	        {
		            for (int i = 0; i < root.content.result.Count; i++)
			        {
			                ResultItem item=root.content.result[i];
                            if (item!=null)
	                        {
                                sqlHelper.getmysqlcom("INSERT INTO `lagou` VALUES ('0', '" + item.adWord + "', '" + item.adjustScore + "', '" 
                                    + item.calcScore + "', '" + item.city + "', '" + item.companyId + "', '" 
                                    + string.Join(",", item.companyLabelList) + "', '" 
                                    + item.companyLogo + "', '" + item.companyName + "', '" + item.companyShortName + "', '" + item.companySize + "', '"
                                    + item.countAdjusted + "', '" + item.createTime + "', '" + item.createTimeSort + "', '" + item.deliverCount + "', '" + item.education + "', '" 
                                    + item.financeStage + "', '" + item.flowScore + "', '" + item.formatCreateTime + "', '" + item.haveDeliver + "', '" 
                                    + item.hrScore + "', '" + item.imstate + "', '" + item.industryField + "', '" + item.jobNature + "', '" 
                                    + item.leaderName + "', '" + item.orderBy + "', '" + item.plus + "', '" + item.positionAdvantage + "', '"
                                    + item.positionFirstType + "', '" + item.positionId + "', '" + item.positionName + "', '" + item.positionType + "', '"
                                    + item.positonTypesMap + "', '" + item.pvScore + "', '" + item.randomScore + "', '" + item.relScore + "', '" 
                                    + item.salary + "', '" + item.score + "', '" + item.searchScore + "', '" + item.showCount + "', '" + item.showOrder + "', '"
                                    + item.totalCount + "', '" + item.workYear + "');");
	                        }
			        }
	        }
            
        }
    }
}
